package com.easy.service.impl;

import com.easy.annotation.LogMethod;
import com.easy.bean.Order;
import com.easy.mapper.OrderMapper;
import com.easy.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Override
    @LogMethod(serviceName = "添加订单", recordParam = true, recordReturn = true)
    public Order add(Order order) {
        orderMapper.add(order);
        Order result = orderMapper.one(order.getId());
        return result;
    }

    @Override
    @LogMethod(serviceName = "删除订单", recordParam = true, recordReturn = true)
    @CacheEvict(value = "order", key = "#p0" ,allEntries = true)//清空value制定的所有实例
    public int del(String id) {
        return orderMapper.del(id);
    }

    @Override
    @LogMethod(serviceName = "编辑订单", recordParam = true, recordReturn = true)
    @CachePut(value = "order", key = "#p0.id")
    public Order edit(Order order) {
        // 2. 设置付款时间（当前时间）
        LocalDateTime now = LocalDateTime.now();
        order.setPayTime(now);
        orderMapper.edit(order);
        Order result = orderMapper.one(order.getId());
        return result;
    }

    @Override
    @Cacheable(value = "order", key = "#p0")//也可以#p0表示第一个参数
    public Order one(String id) {
        return orderMapper.one(id);
    }
}
